#include <gtk/gtk.h>
#include <string.h>
#include "event_types.h"

#define UI_PATH "./entry.ui"
#define MAIN_WINDOW "window"
#define ENTRY "entry"
#define CHK_EDITABLE "chk_editable"
#define CHK_VISIBLE "chk_visible"

static GtkWidget *w;
static GtkWidget *s_entry;
static GtkWidget *s_dialog_font = NULL;

gboolean motion_handle(GtkWidget *self,GdkEvent *event,gpointer user_data){
	printf("enter %s\n",__func__);
	return FALSE;
}

gboolean event_handle(GtkWidget *self,GdkEvent *event,gpointer user_data){
	GdkEventType type = event->type;
	const char *str = NULL;
	for(int i=0;i<TYPES_COUNT;i++){
		if(s_types[i].type == type){
			str = s_types[i].type_string;
			break;
		}
	}
	if(!str) str = "NO_NAME";
	printf("%s:[self:%p]event type is %s(%d)[ew:%p]\n",__func__,
		gtk_widget_get_window(self),str,type,event->any.window);
	//if(type == GDK_KEY_PRESS)
		//return TRUE;
	//else
		return FALSE;
}

void font_selection_ok(GtkWidget *button,gpointer user_data){
	GtkStyle *style;
	//GdkFont *font;

	//gchar *font_name = gtk_font_selection_dialog_get_font_name(GTK_IS_FONT_SELECTION_DIALOG(s_dialog_font));
	//printf("%s:font_name is %s\n",__func__,font_name);
	/*
	font = gtk_font_selection_dialog_get_font(GTK_IS_FONT_SELECTION_DIALOG(s_dialog_font));
	if(font){
		style = gtk_style_copy(gtk_widget_get_style(s_entry));
		gdk_font_unref(style->font);
		style->font = font;
		gdk_font_ref(style->font);
		gtk_widget_set_style(s_entry,style);
	}
	*/
}

void font_changed(GtkWidget *self,gpointer user_data){
	s_dialog_font = gtk_font_chooser_dialog_new("Font Selection Dialog :)",GTK_WINDOW(w));
	gtk_window_set_transient_for(GTK_WINDOW(s_dialog_font),GTK_WINDOW(w));
	gtk_window_set_destroy_with_parent(GTK_WINDOW(s_dialog_font),TRUE);
	gtk_font_chooser_set_font(GTK_FONT_CHOOSER(s_dialog_font),NULL);
	if(gtk_dialog_run(GTK_DIALOG(s_dialog_font)) == GTK_RESPONSE_OK){
		gchar *font_name = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(s_dialog_font));
		printf("%s:font_name is %s\n",__func__,font_name);
		PangoFontDescription *pfd = pango_font_description_from_string(font_name);
		//gtk_widget_modify_font(s_entry,pfd);
		gtk_widget_override_font(s_entry,pfd);
		pango_font_description_free(pfd);
	}
	gtk_widget_destroy(s_dialog_font);
/*
	if(!s_dialog_font){
		s_dialog_font = gtk_font_selection_dialog_new("Font Selection Dialog :)");
		g_return_if_fail(GTK_IS_FONT_SELECTION_DIALOG(s_dialog_font));
		//gtk_window_position(GTK_WINDOW(s_dialog_font),GTK_WIN_POS_MOUSE);
		gtk_window_set_position(GTK_WINDOW(s_dialog_font),GTK_WIN_POS_MOUSE);
		g_signal_connect(s_dialog_font,"destroy",
			G_CALLBACK(gtk_widget_destroyed),&s_dialog_font);
		g_signal_connect(gtk_font_selection_dialog_get_ok_button(
			GTK_IS_FONT_SELECTION_DIALOG(s_dialog_font)),
			"clicked",G_CALLBACK(font_selection_ok),NULL);
		g_signal_connect_swapped(gtk_font_selection_dialog_get_cancel_button(
			GTK_IS_FONT_SELECTION_DIALOG(s_dialog_font)),
			"clicked",G_CALLBACK(gtk_widget_destroy),s_dialog_font);
	}
	//if(!GTK_WIDGET_VISIBLE(s_dialog_font))
	if(!gtk_widget_get_visible(s_dialog_font))
		gtk_widget_show(s_dialog_font);
	else
		gtk_widget_destroy(s_dialog_font);
*/
}

void editable_toggled(GtkWidget *self,GtkEntry *entry){
	//gtk_entry_set_editable(entry,GTK_TOGGLE_BUTTON(self)->active);
	printf("%s:enter function\n",__func__);
	gtk_editable_set_editable((GtkEditable*)entry,gtk_toggle_button_get_active(
		GTK_TOGGLE_BUTTON(self)));
}

void visible_toggled(GtkWidget *self,GtkEntry *entry){
	//gtk_entry_set_visibility(entry,GTK_TOGGLE_BUTTON(self)->active);
	printf("%s:enter function\n",__func__);
	gtk_entry_set_visibility(entry,gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(self)));
}

void entry_changed(GtkWidget *self,gpointer user_data){
	gchar *text = (gchar*)gtk_entry_get_text(GTK_ENTRY(self));
	printf("%s:text is %s\n",__func__,text);
	text = g_ascii_strup(text,strlen(text));
	gtk_entry_set_text(GTK_ENTRY(self),text);
}

int main(int argc,char **argv){
	//GtkWidget *w;
	GError *error = NULL;
	gtk_init(&argc,&argv);
	GtkBuilder *builder = gtk_builder_new();
	if(!gtk_builder_add_from_file(builder,UI_PATH,&error)){
		g_critical("Couldn't load builder file: %s",error->message);
		g_error_free(error);
	}

	w = GTK_WIDGET(gtk_builder_get_object(builder,MAIN_WINDOW));
	if(!w){
		g_critical("Widget \"%s\" is missing in file %s",MAIN_WINDOW,UI_PATH);
	}
	gtk_window_set_title(GTK_WINDOW(w),"clist todo list");
	gtk_window_set_default_size(GTK_WINDOW(w),300,100);
	gtk_window_set_position(GTK_WINDOW(w),GTK_WIN_POS_CENTER_ALWAYS);
	gtk_container_set_border_width(GTK_CONTAINER(w),5);

	s_entry = GTK_WIDGET(gtk_builder_get_object(builder,ENTRY));
	gtk_widget_unrealize(s_entry);
	//gtk_widget_realize(s_entry);
	//gtk_widget_show(s_entry);
	//gtk_widget_map(s_entry);

	//gtk_widget_set_events(s_entry,gtk_widget_get_events(s_entry)^GDK_BUTTON_MOTION_MASK);
	gboolean is_r = gtk_widget_get_realized(s_entry);
	printf("is_r %d\n",is_r);
	GdkEventMask mask = gtk_widget_get_events(s_entry);
	printf("%s:%08x\n",__func__,mask);
	//mask &= ~0xffffffffffffffff;
	//mask &= ~GDK_KEY_PRESS_MASK;
	mask &= ~GDK_BUTTON_MOTION_MASK;
	printf("const mask is %08x\n",GDK_KEY_PRESS_MASK);
	printf("%s:%08x\n",__func__,mask);
	gtk_widget_set_events(s_entry,mask|GDK_BUTTON_MOTION_MASK|
		GDK_POINTER_MOTION_HINT_MASK);
	gtk_widget_realize(s_entry);
	mask = gtk_widget_get_events(s_entry);
	printf("# %s:%08x\n",__func__,mask);

	//gtk_window_set_events(GTK_WINDOW(s_entry),mask);

	gtk_builder_connect_signals(builder,NULL);
	g_object_unref(builder);

	gtk_widget_show_all(w);
	GdkCursor *cursor = gdk_cursor_new(GDK_CLOCK);
	gdk_window_set_cursor(gtk_widget_get_window(w),cursor);
	//gdk_cursor_destroy(cursor);
	g_object_unref(G_OBJECT(cursor));


	gtk_main();
	return 0;
}